<?php

namespace App\Console\Commands;

use App\Models\ShopSellRank;
use App\Services\ConfigService;
use App\Services\GoodService;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class SellShopCommand extends Command {
	/**
	 * The name and signature of the console command.
	 *
	 * @var string
	 */
	protected $signature = 'sell:shop';

	/**
	 * The console command description.
	 *
	 * @var string
	 */
	protected $description = 'The store sales ranking statistics';

	/**
	 * Create a new command instance.
	 *
	 * @return void
	 */
	public function __construct() {
		parent::__construct();
	}

	/**
	 * Execute the console command.
	 *
	 * @return mixed
	 */
	public function handle() {
		try {
			ShopSellRank::truncate();
			$priceSet = ConfigService::generatePrice();
			//A price shop sales ranking.
			foreach ($priceSet as $key => $price) {
				$sellNumbers = GoodService::shopRank($price['start_price'], $price['end_price']);
				if (!$sellNumbers) {
					continue;
				}
				//Record sales ranking table.
				$ret = GoodService::insertShopRank($sellNumbers, $price);
				print("当前第" . $key . "条数据\n");
			}
		} catch (\Excepiton $e) {
			Log::error("Write db error:" . $e->getMessage());
		}
		print "Execute successfully\n";

	}
}
